package com.xc.hotelmgr.webapi.modules.op.controller;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.xc.hotelmgr.webapi.common.annotation.SysLog;
import com.xc.hotelmgr.webapi.modules.dt.entity.DtEntity;
import com.xc.hotelmgr.webapi.modules.dt.entity.StoreEntity;
import com.xc.hotelmgr.webapi.modules.dt.service.DtService;
import com.xc.hotelmgr.webapi.modules.dt.service.StoreService;
import com.xc.hotelmgr.webapi.modules.op.cache.CommPriceCache;
import com.xc.hotelmgr.webapi.modules.sys.controller.AbstractController;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xc.hotelmgr.webapi.modules.op.entity.CommPriceEntity;
import com.xc.hotelmgr.webapi.modules.op.service.CommPriceService;
import com.xc.hotelmgr.webapi.common.utils.PageUtils;
import com.xc.hotelmgr.webapi.common.utils.R;



/**
 * 
 *
 * @author kemmy
 * @email kemmy@gmail.com
 * @date 2020-09-29 14:30:31
 */
@RestController
@RequestMapping("op/commprice")
@RequiredArgsConstructor
public class CommPriceController extends AbstractController {

    private final CommPriceService commPriceService;

    private final DtService dtService;

    private final CommPriceCache commPriceCache;

    private final StoreService storeService;

    /**
     * 查询机构树
     */
    @SysLog
    @RequestMapping("/listdt")
    @RequiresPermissions("op:commprice:list")
    public R listDt(@RequestParam Map<String, Object> params){
        params.put("dtId",getUserDtId());
        PageUtils page = dtService.queryPage(params);
        dtService.setParentName( (List<DtEntity>) page.getList());
        return R.ok().put("page", page);
    }
    /**
     * 列表
     */
    @SysLog
    @RequestMapping("/list")
    @RequiresPermissions("op:commprice:list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("dtId",getUserDtId());
        PageUtils page = commPriceService.sqlQuryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @SysLog
    @RequestMapping("/info/{id}")
    @RequiresPermissions("op:commprice:info")
    public R info(@PathVariable("id") Integer id){
		CommPriceEntity commPrice = commPriceService.getById(id);

        return R.ok().put("commPrice", commPrice);
    }

    /**
     * 保存
     */
    @SysLog
    @RequestMapping("/save")
    @RequiresPermissions("op:commprice:save")
    public R save(@RequestBody CommPriceEntity commPrice){
		commPriceService.save(commPrice);
		int storeId = commPrice.getStoreId();
        boolean result = commPriceCacheInit(storeId);
        if(result) {
            return R.ok();
        }
        return R.error("缓存添加失败");
    }

    /**
     * 修改
     */
    @SysLog
    @RequestMapping("/update")
    @RequiresPermissions("op:commprice:update")
    public R update(@RequestBody CommPriceEntity commPrice){
		commPriceService.updateById(commPrice);
        int storeId = commPrice.getStoreId();
        boolean result = commPriceCacheInit(storeId);
        if(result) {
            return R.ok();
        }
        return R.error("缓存添加失败");
    }

    @SysLog
    @RequestMapping("/delete")
    @RequiresPermissions("op:commprice:delete")
    public R delete(@RequestBody Map params){
        Integer id =  (Integer)params.get("id");
        Integer storeId = (Integer) params.get("storeId");
        commPriceService.removeById(id);
        boolean result = commPriceCacheInit(storeId);
        if(result) {
            return R.ok();
        }
        return R.error("缓存添加失败");
    }

    private boolean commPriceCacheInit(Integer storeId) {
        try {
            StoreEntity storeEntity = storeService.getById(storeId);
            String appKey = storeEntity.getAppkey();
            List<CommPriceEntity> commPriceEntities = commPriceService.getByStoreId(storeId);
            if(commPriceEntities.isEmpty()) {
                commPriceCache.del(appKey);
                return true;
            }
            commPriceCache.addOrUpdate(appKey, JSONObject.toJSONString(commPriceEntities));
        }catch (RuntimeException e){
            logger.error(e.getMessage());
            return false;
        }
        return true;
    }

}
